package com.atguigu.tingshu.user.api;

import cn.hutool.core.bean.BeanUtil;
import com.atguigu.tingshu.common.login.GuiguLogin;
import com.atguigu.tingshu.common.result.Result;
import com.atguigu.tingshu.common.util.AuthContextHolder;
import com.atguigu.tingshu.user.service.UserInfoService;
import com.atguigu.tingshu.vo.user.UserInfoVo;
import com.atguigu.tingshu.vo.user.UserPaidRecordVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@Tag(name = "用户管理接口")
@RestController
@RequestMapping("api/user")
@SuppressWarnings({"all"})
public class UserInfoApiController {

    @Autowired
    private UserInfoService userInfoService;

    /**
     * 根据用户ID查询用户信息
     *
     * @param userId
     * @return
     */
    @Operation(summary = "根据用户ID查询用户信息")
    @GetMapping("/userInfo/getUserInfoVo/{userId}")
    public Result<UserInfoVo> getUserInfoVo(@PathVariable Long userId) {
        UserInfoVo userInfoVo = userInfoService.getUserInfo(userId);
        return Result.ok(userInfoVo);
    }

    /**
     * 判断提交的声音id购买状态
     *
     * @param userId                         用户id
     * @param albumId                        专辑id
     * @param needCheckPayStatussTrackIdList 需要检查的购买状态的声音id列表
     * @return {
     * "声音id":购买状态
     * "声音id":购买状态
     * "声音id":购买状态
     * }
     * 1：已购买 0：未购买
     */
    @Operation(summary = "判断提交的声音id购买状态")
    @PostMapping("/userInfo/userIsPaidTrack/{userId}/{albumId}")
    public Result<Map<Long, Integer>> userIsPaidTrack(
            @PathVariable Long userId,
            @PathVariable Long albumId,
            @RequestBody List<Long> needCheckPayStatussTrackIdList
    ) {
        Map<Long, Integer> map = userInfoService.userIsPaidTrack(userId, albumId, needCheckPayStatussTrackIdList);
        return Result.ok(map);
    }

    /**
     * 判断当前用户是否购买了指定专辑
     *
     * @param albumId
     * @return
     */
    @GuiguLogin
    @Operation(summary = "判断当前用户是否购买过专辑")
    @GetMapping("/userInfo/isPaidAlbum/{albumId}")
    public Result<Boolean> isPaidAlbum(@PathVariable Long albumId) {
        Long userId = AuthContextHolder.getUserId();
        Boolean result = userInfoService.isPaidAlbum(userId, albumId);
        return Result.ok(result);
    }

    /**
     * 查询当前用户指定专辑下已购声音列表
     *
     * @param albumId
     * @return
     */
    @GuiguLogin
    @Operation(summary = "查询当前用户指定专辑下已购声音列表")
    @GetMapping("/userInfo/findUserPaidTrackList/{albumId}")
    public Result<List<Long>> findUserPaidTrackIdList(@PathVariable Long albumId) {
        Long userId = AuthContextHolder.getUserId();
        List<Long> userPaidTrackIdList = userInfoService.findUserPaidTrackIdList(userId, albumId);
        return Result.ok(userPaidTrackIdList);
    }

    /**
     * 用户付款成功后，给用户进行虚拟物品的发货
     * 这里不能加@GuiguLogin，
     * 因为用户付款方式如果是微信支付，微信支付回调之后不会带上用户信息
     *
     * @param userPaidRecordVo
     * @return
     */
    //@GuiguLogin
    @Operation(summary = "用户付款成功后，给用户进行虚拟物品的发货")
    @PostMapping("/userInfo/savePaidRecord")
    public Result savePaidRecord(@RequestBody UserPaidRecordVo userPaidRecordVo) {
        userInfoService.savePaidRecord(userPaidRecordVo);
        return Result.ok();
    }

}

